package com.tvunetworks.center.user.controller;

import com.github.pagehelper.PageInfo;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.model.param.AddFeatureParam;
import com.tvunetworks.center.user.model.vo.FeatureVo;
import com.tvunetworks.center.user.service.FeatureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @program: tvucc-aggregator
 * @description: FeatureController
 * @author lebronchen
 * @create: 2019-06-10 16:48
 **/
@RestController
@RequestMapping("/feature")
@Slf4j
public class FeatureController {

    @Autowired
    private FeatureService featureService;

    /**
     *
     * @return Result
     */
    @GetMapping("/listAll")
    public Result<Object> listFeature(){
        List<FeatureVo> features = featureService.listAll();
        return ResultUtil.success(features);
    }

//    /**
//     * 创建用户的时候 可选的feature列表
//     * 用于创建用户时 可勾选列表
//     * @param loginUser
//     * @param role
//     * @return
//     */
//    @GetMapping("/listFeatureForCreate")
//    public Result<Object> listFeatureForCreate(@Logined LoginUser loginUser, @RequestParam String role, String ownerId){
//        if (loginUser == null) {
//            return ResultUtil.noLogin();
//        }
//        Map<String, List<FeatureVo>> features = featureService.listFeatureForCreate(loginUser, role, ownerId);
//        return ResultUtil.success(features);
//    }
//
//    /**
//     * 修改用户的时候 可选的feature列表
//     * @param loginUser
//     * @param modifyUserId
//     * @return
//     */
//    @GetMapping("/listFeatureForModify")
//    public Result<Object> listFeatureForModify(@Logined LoginUser loginUser, @RequestParam String modifyUserId){
//        if (loginUser == null) {
//            return ResultUtil.noLogin();
//        }
//        Map<String, List<FeatureVo>> features = featureService.listFeatureForModify(loginUser, modifyUserId);
//        return ResultUtil.success(features);
//    }

    /**
     *
     * @param param param
     * @return  Result
     */
    @PostMapping("/add")
    public Result<Object> addFeature(@Valid AddFeatureParam param){
        int result = featureService.addFeature(param);
        if (result == 1) {
            return ResultUtil.error("81001101", "Feature exist");
        }
        return ResultUtil.success();
    }

    /**
     *
     * @param id id
     * @param param param
     * @return Result
     */
    @PostMapping("/update")
    public Result<Object> updateFeature(@RequestParam String id, @Valid AddFeatureParam param){
        int result = featureService.updateFeature(id, param);
        if (result == 1) {
            return ResultUtil.error("81001201", "Feature not exist");
        } else if (result == 2) {
            return ResultUtil.error("81001202", "Feature exist");
        }
        return ResultUtil.success();
    }

    /**
     *
     * @param id id
     * @return Result
     */
    @PostMapping("/delete")
    public Result<Object> deleteFeature(@RequestParam String id){
        int result = featureService.deleteFeature(id);
        if( result == 1) {
            return ResultUtil.error("81001301", "Feature not exist");
        } else if (result == 2) {
            return ResultUtil.error("81001302", "Exist menu use this feature");
        }
        return ResultUtil.success();
    }

    /**
     *
     * @param pageNum pageNum
     * @param pageSize pageSize
     * @return Result
     */
    @GetMapping("/page")
    public Result<Object> pageFeature(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize){
        PageInfo<FeatureVo> features = featureService.pageFeature(pageNum, pageSize);
        return ResultUtil.success(features);
    }
}
